//
//  FANEmoticonView.m
//  Weibo - OC
//
//  Created by user on 16/4/27.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANEmoticonView.h"
#import "FANEmoticonStackView.h"
#import "FANEmoticonViewModel.h"
#import "FANEmoticonCell.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>


@interface FANEmoticonView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) FANEmoticonStackView *emoticonStackView;

@property (nonatomic, strong) UICollectionView *emoticonCollectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation FANEmoticonView

- (UIPageControl *)pageControl{
    
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] init];
        
//        _pageControl.numberOfPages = 5;
//        
//        _pageControl.pageIndicatorTintColor = [UIColor redColor];
//        
//        _pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
//        
//        _pageControl.currentPage = 1;
        
        [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKey:@"_currentPageImage"];
        
        [_pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKey:@"_pageImage"];
        
        _pageControl.userInteractionEnabled = false;
        
        _pageControl.hidden = true;
    }
    
    return _pageControl;
}

- (UICollectionViewFlowLayout *)flowLayout{
    
    if (!_flowLayout) {
        
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _flowLayout.minimumInteritemSpacing = 0;
        
        _flowLayout.minimumLineSpacing = 0;
        
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    }
    return _flowLayout;
}

- (UICollectionView *)emoticonCollectionView{
    
    if (!_emoticonCollectionView) {
    
        _emoticonCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        
        _emoticonCollectionView.dataSource = self;
        
        _emoticonCollectionView.delegate = self;
        
        _emoticonCollectionView.pagingEnabled = true;
        
        _emoticonCollectionView.bounces = false;
        
        _emoticonCollectionView.showsHorizontalScrollIndicator = false;
    }
    return _emoticonCollectionView;
}

- (FANEmoticonStackView *)emoticonStackView{
    
    if (!_emoticonStackView) {
        
        _emoticonStackView = [[FANEmoticonStackView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        
        _emoticonStackView.stackBlock = ^(FANEmoticonStackType type){
            
//            NSLog(@"%ld",type);
            NSIndexPath *indexPathf = [NSIndexPath indexPathForItem:0 inSection:type];
            
            [weakSelf.emoticonCollectionView scrollToItemAtIndexPath:indexPathf atScrollPosition:UICollectionViewScrollPositionLeft animated:false];
            
            [weakSelf showPageControl:indexPathf];
            
        };
    
    }

    return  _emoticonStackView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint center = scrollView.center;
    
    center.x = scrollView.center.x + scrollView.contentOffset.x;
    
    NSArray *cells = self.emoticonCollectionView.visibleCells;
    
    NSIndexPath *indexPathF;
    
    if (cells.count == 2) {
        
        FANEmoticonCell *firstCell = cells.firstObject;
        
        FANEmoticonCell *lastCell = cells.lastObject;
        
        if (CGRectContainsPoint(firstCell.frame, center)) {
            
            indexPathF = [self.emoticonCollectionView indexPathForCell:firstCell];
            
        }else{
            
            indexPathF = [self.emoticonCollectionView indexPathForCell:lastCell];
        }
        
    }else{
        
        indexPathF = [self.emoticonCollectionView indexPathForCell:cells.firstObject];
        
    }
    
    self.emoticonStackView.selectedSection = (int)indexPathF.section;
    
    [self showPageControl:indexPathF];
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, self.frame.size.height - self.emoticonStackView.frame.size.height);

}

- (void)setupUI{
    
    self.backgroundColor = [UIColor orangeColor];
    
    [self addSubview:self.emoticonStackView];
    
    [self addSubview:self.emoticonCollectionView];
    
    [self addSubview:self.pageControl];
    
    [self.emoticonStackView makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.leading.trailing.equalTo(self);
        
        make.height.equalTo(37);
        
    }];
    
    [self.emoticonCollectionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.trailing.equalTo(self);
        
        make.bottom.equalTo(self.emoticonStackView.top);
        
    }];
    
    [self.pageControl makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.centerX);
        
        make.bottom.equalTo(self.emoticonStackView.top).offset(4);
        
    }];
    
    [self.emoticonCollectionView registerClass:[FANEmoticonCell class] forCellWithReuseIdentifier:@"emoticonCell"];
    
}

- (void)showPageControl:(NSIndexPath *)indexPathf{
    
    self.pageControl.numberOfPages = [[FANEmoticonViewModel sharedEmoticon].allEmoticons[indexPathf.section] count];
    
    self.pageControl.currentPage = indexPathf.item;
    
    self.pageControl.hidden = indexPathf.section == 0;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
//    NSLog(@"%ld",[FANEmoticonViewModel sharedEmoticon].allEmoticons.count);
    
    return [FANEmoticonViewModel sharedEmoticon].allEmoticons.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
//    NSLog(@"%ld",[[FANEmoticonViewModel sharedEmoticon].allEmoticons[section] count]);
    
    return [[FANEmoticonViewModel sharedEmoticon].allEmoticons[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FANEmoticonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"emoticonCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.emoticonsArray = [FANEmoticonViewModel sharedEmoticon].allEmoticons[indexPath.section][indexPath.item];
    
    cell.indexPathF = indexPath;
    
//    cell.emoticonBlock = ^(FANEmoticonButton *button){
//      
//        [self didEmoticonClick:button];
//        
//    };
    
    return cell;
}

//- (void)didEmoticonClick:(FANEmoticonButton *)button{
//    
////    NSLog(@"点了表情按钮");
//    
//    if ([button.emoticon.type isEqualToString:@"1"]) {
//        
//        
//    }else{
//        
//        
//    }
//}

@end
